package com.yyjj.zixun.fragmnet;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aries.ui.view.title.TitleBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yyjj.zixun.R;
import com.yyjj.zixun.adapter.ArticleAdapter;
import com.yyjj.zixun.entity.ArticleBean;
import com.yyjj.zixun.server.NewsBack;
import com.yyjj.zixun.server.NewsGlobal;

import java.util.List;

import tech.com.commoncore.base.BaseTitleFragment;
import tech.com.commoncore.manager.GlideManager;

public class ArticleFragment extends BaseTitleFragment {
    private static final String ARG_PARAM1 = "mParam1";
    private static final String ARG_PARAM2 = "mParam2";

    private String mParam;
    private Boolean showBanner;

    private ImageView bannerImg;
    private TextView bannerTitle;

    private SmartRefreshLayout smartRefresh;
    private RecyclerView contentRecycler;
    private BaseQuickAdapter articleAdapter;

    private View bannerLayout;

    private List<ArticleBean> contentList;

    private int currentPage = 1;

    public static ArticleFragment newInstance(String param, boolean showBanner) {
        ArticleFragment fragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param);
        args.putBoolean(ARG_PARAM2, showBanner);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam = getArguments().getString(ARG_PARAM1);
            showBanner = getArguments().getBoolean(ARG_PARAM2);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_article;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        bannerImg = mContentView.findViewById(R.id.news_banner_img);
        bannerTitle = mContentView.findViewById(R.id.news_banner_title);
        bannerLayout = mContentView.findViewById(R.id.news_banner_layout);
        smartRefresh = mContentView.findViewById(R.id.smart_refresh_layout);
        contentRecycler = mContentView.findViewById(R.id.content_recycler_view);

//        Drawable drawable = mContext.getResources().getDrawable(R.drawable.divider_recycler_vertical);
//        DividerItemDecoration decoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
//        decoration.setDrawable(drawable);
//        contentRecycler.addItemDecoration(decoration);
        articleAdapter = new ArticleAdapter(R.layout.news_layout_item_article);
        contentRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        contentRecycler.setAdapter(articleAdapter);

        if (showBanner) {
            setBannerData();
        }

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                requestData(mParam);
                currentPage++;
            }
        });

        bannerImg.setImageResource(R.mipmap.news_icon_banner_1);

        if (contentList == null || contentList.isEmpty()) {
            smartRefresh.autoRefresh();
        }
    }

    private void setBannerData() {
        NewsGlobal.getNewsImp().requestArticle(currentPage, 1, "post_like", mParam, new NewsBack<List<ArticleBean>>() {
            @Override
            public void onBack(List<ArticleBean> articleBeans, String err) {
                smartRefresh.finishRefresh();
                if (err == null && articleBeans != null && !articleBeans.isEmpty()) {
                    bannerLayout.setVisibility(View.VISIBLE);
                    final ArticleBean articleBean = articleBeans.get(0);
                    ArticleBean.More more = articleBean.getMore();
                    if (more != null) {
                        GlideManager.loadImg(more.getThumbnai(), bannerImg);
                    }
                    bannerTitle.setText(articleBean.getPost_title());
                    bannerLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            ARouter.getInstance().build(NEWS_ARTICLE_DETAIL)
//                                    .withInt(NEWS_ARTICLE_KEY, articleBean.getId())
//                                    .navigation();
                        }
                    });
                }
            }
        });
    }

    private void requestData(String category) {
        NewsGlobal.getNewsImp().requestArticle(currentPage, 20, "id", category, new NewsBack<List<ArticleBean>>() {
            @Override
            public void onBack(List<ArticleBean> articleBeans, String err) {
                smartRefresh.finishRefresh();
                if (contentList == null) {
                    contentList = articleBeans;
                } else {
                    contentList.addAll(articleBeans);
                }
                updateView();
            }
        });
    }

    private void updateView() {
        if (contentList != null && !contentList.isEmpty()) {
            articleAdapter.replaceData(contentList);
            articleAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(getResources().getString(R.string.zixun_app_name)).setBackground(getResources().getDrawable(R.drawable.bg_titlebar_jb));
    }
}
